/***************************************************************************
 *
 *   Copyright (C) 2007 by Willem van Straten
 *   Licensed under the Academic Free License version 2.1
 *
 ***************************************************************************/

#include "dsp/LoadToFoldConfig.h"
#include <algorithm>

using namespace std;

dsp::LoadToFold::Config::Config ()
{
  can_cuda = true;
  can_thread = true;

  // phase-locked filterbank phase bins
  plfb_nbin = 0;
  // phase-locked filterbank channels
  plfb_nchan = 0;

  // default cyclic spectrum off
  cyclic_nchan = 0;
  // default to no oversampling
  cyclic_mover = 1;

  // do not compute the fourth order moments by default
  fourth_moment = false;

  // do not produce pdmp output by default
  pdmp_output = false;

  // do not use spectral kurtosis filterbank by default
  sk_zap = false;

  // when applying spectral kurtosis, also produce non-zapped version of output
  nosk_too = false;

  // first channel to conduct spectral kurtosis detection
  sk_chan_start = 0;

  // last channel to conduct spectral kurtosis detection
  sk_chan_end = 0;

  // by default onl 1 SK thread [per CPU thread]
  sk_nthreads = 1;

  // by default, do not fold the SK filterbank output
  sk_fold = false;

  // full polarization by default
  npol = 4;

  // let Fold choose the number of bins
  nbin = 0;

  // an optimization feature used in Detection
  ndim = 4;

  // Don't allow more bins than is sensible
  force_sensible_nbin = false;

  // unload sub-integrations to separate files by default
  single_archive = false;

  // if specified, the number of sub-integrations to write to each file
  subints_per_archive = 0;

  // integrate for specified number of pulses
  integration_turns = 0;

  // integrate for specified interval length
  integration_length = 0;

  // by default, no minimum is specified
  minimum_integration_length = -1;

  // rotate the pulsar with respect to the predictor
  reference_phase = 0;

  // fold at a constant period
  folding_period = 0;

  // do not fold the fractional pulses at the beginning and end of data
  fractional_pulses = false;

  // do not fold asynchronously by default
  asynchronous_fold = false;

  // produce BasebandArchive output by default
  archive_class = "Baseband";
  archive_class_specified_by_user = false;

  // Output dynamic extensions by default
  dynamic_extensions = true;
}

//! Add command line options
void dsp::LoadToFold::Config::add_options (CommandLine::Menu& menu)
{
  SingleThread::Config::add_options(menu);
  dedisp.add_options(menu);
}

unsigned dsp::LoadToFold::Config::get_nfold () const
{
  size_t nfold = 1 + additional_pulsars.size();

  nfold = std::max( nfold, predictors.size() );
  nfold = std::max( nfold, ephemerides.size() );

  return nfold;
}

// set the spectral kurtosis window lengths
void dsp::LoadToFold::Config::set_sk_m (string txt)
{
  sk_m.resize(0);

  while (txt != "")
  {
    string dev = stringtok (txt, ",");
    sk_m.push_back( fromstring<unsigned>(dev) );
  }
}

// set the spectral kurtosis window overlap factors
void dsp::LoadToFold::Config::set_sk_noverlap (string txt)
{
  sk_noverlap.resize(0);

  while (txt != "")
  {
    string dev = stringtok (txt, ",");
    sk_noverlap.push_back( fromstring<unsigned>(dev) );
  }
}

// set the spectral kurtosis window overlap factors
void dsp::LoadToFold::Config::set_sk_std_devs (string txt)
{
  sk_std_devs.resize(0);

  while (txt != "")
  {
    string dev = stringtok (txt, ",");
    sk_std_devs.push_back( fromstring<float>(dev) );
  }
}

void dsp::LoadToFold::Config::set_archive_class (const std::string& name)
{
  archive_class = name;
  archive_class_specified_by_user = true;
}

/*
  These headers are required only for setting verbosity.
*/

#include "dsp/Archiver.h"
#include "Pulsar/Archive.h"

void dsp::LoadToFold::Config::set_quiet ()
{
  SingleThread::Config::set_quiet();
  Pulsar::Archive::set_verbosity (0);
  dsp::Archiver::verbose = 0;
}

void dsp::LoadToFold::Config::set_verbose ()
{
  SingleThread::Config::set_verbose();
  Pulsar::Archive::set_verbosity (2);
  dsp::Archiver::verbose = 2;
}

void dsp::LoadToFold::Config::set_very_verbose ()
{
  SingleThread::Config::set_very_verbose();
  Pulsar::Archive::set_verbosity (3);
  dsp::Archiver::verbose = 3;
}
